package com.mobike.android.view

import android.animation.ValueAnimator
import android.app.Activity
import android.app.Dialog
import android.os.Build
import android.view.MenuItem
import android.view.animation.DecelerateInterpolator
import com.android.basic.ktExtension.flyn.Eyes


/**
 * this is the rounding method used by Android itself.
 */

val lightStatusBarSupported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M

private val brandSmall = Build.BRAND.toLowerCase()

private var isMiUi: Boolean = true
private var isFlyme: Boolean = true

fun Activity.checkLightStatusBarSupportCompat() {
  if (brandSmall.contains("xiaomi") && isMiUi) {
    isMiUi = Eyes.MIUISetStatusBarLightMode(this, true)
  } else if (brandSmall.contains("meizu") && isFlyme) {
    isFlyme = Eyes.FlymeSetStatusBarLightMode(this, true)
  }
}

val displayDensity get() = displayMetrics.density
inline val Int.dpFloat: Float get() = displayDensity * this + 0.5F
inline val Float.dpFloat: Float get() = displayDensity * this + 0.5F
inline val Int.dp: Int get() = (displayDensity * this + 0.5F).toInt()
inline val Float.dp: Int get() = (displayDensity * this + 0.5F).toInt()

inline val Int.sp: Float get() = (displayMetrics.scaledDensity * this + 0.5F)
inline val Float.sp: Float get() = (displayMetrics.scaledDensity * this + 0.5F)

val screenHeight get() = displayMetrics.heightPixels
val screenWidth get() = displayMetrics.widthPixels

// TODO(later) does status bar height change on a device??? we don't know
val statusBarHeight: Int by lazy {
  val resourceId = res.getIdentifier("status_bar_height", "dimen", "android")
  if (resourceId > 0) {
    res.getDimensionPixelSize(resourceId)
  } else {
    25.dp
  }
}

// TODO(later) does status bar height change on a device??? we don't know
val navigationBarHeight: Int by lazy {
  var result = 0
  val resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android")
  if (resourceId > 0) {
    result = res.getDimensionPixelSize(resourceId)
  }
  result
}

val hasNavigationBar: Boolean by lazy {
  val id = res.getIdentifier("config_showNavigationBar", "bool", "android")
  id > 0 && res.getBoolean(id)
}

fun MenuItem.alwaysShowAsAction(): MenuItem {
  this.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
  return this
}


fun animateDialogDelayedShown(p: Dialog, animateDim: Boolean, finalDimOverride: Float? = null) {
  if (p.isShowing) {
    val animator = ValueAnimator.ofFloat(0F, 1F)
    val lp = p.window.attributes
    lp.alpha = 0f
    val finalDim = finalDimOverride ?: lp.dimAmount
    if (animateDim) lp.dimAmount = 0f
    p.window.attributes = lp
    animator.addUpdateListener { animation ->
      try {
        val lp1 = p.window.attributes
        lp1.alpha = animation.animatedFraction
        if (animateDim) lp1.dimAmount = finalDim
        p.window.attributes = lp1
      } catch (e: Exception) {
        animator.cancel()
      }
    }
    animator.startDelay = 400
    animator.duration = 150
    animator.interpolator = DecelerateInterpolator(1.5f)
    animator.start()
  }
}